package io.dshubs.archives.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dshubs.archives.dao.ArchiveBaseClassMapper;
import io.dshubs.archives.dto.ArchiveBaseCategoryDto;
import io.dshubs.archives.entity.ArchiveBaseCategory;
import io.dshubs.archives.service.ArchiveBaseCategoryService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.mybatis.annotation.DataFilter;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.utils.ConvertUtils;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;

import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.dshubs.dto.SysDictDataDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 全宗类信息表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-16
 */

@RestController
@Api(tags="全宗类别管理")
@RequestMapping("/class")
public class ArchiveBaseCategoryController {

    @Autowired
    private ArchiveBaseCategoryService archiveBaseCategoryService;

    @Autowired
    private ArchiveBaseClassMapper archiveBaseClassMapper;


    @GetMapping("list")
    @ApiOperation("全宗类别:list")
    @LogOperation("全宗类别:list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "archiveId", value = "全宗id", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "name", value = "分类名称", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "code", value = "分类号", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "archiveCode", value = "分类号", paramType = "query", dataType="String")

    })
    @PreAuthorize("hasAuthority('archive:class:page')")


    public Result<List<ArchiveBaseCategoryDto>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        List<ArchiveBaseCategoryDto> list = archiveBaseCategoryService.list(params);

        return new Result<List<ArchiveBaseCategoryDto>>().ok(list);
    }

    @GetMapping("/list/no-tree")
    @ApiOperation("列表")
    @LogOperation("全宗类别:listNoTree")
    public Result<List<ArchiveBaseCategory>> listNoTree(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<List<ArchiveBaseCategory>>().ok(archiveBaseCategoryService.listNoTree(params));
    }


    @GetMapping("{id}")
    @ApiOperation("详情")
    @LogOperation("全宗类别:get")
    public Result<ArchiveBaseCategory> get(@PathVariable("id") Long id){
        return new Result<ArchiveBaseCategory>().ok(archiveBaseCategoryService.selectById(id));
    }

    @GetMapping("/client")
    public Result<ArchiveBaseCategoryDto> getById(@RequestBody Long id){
        ArchiveBaseCategoryDto archiveBaseCategoryDto = ConvertUtils.sourceToTarget(archiveBaseCategoryService.selectById(id), ArchiveBaseCategoryDto.class);
        return new Result<ArchiveBaseCategoryDto>().ok(archiveBaseCategoryDto);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("全宗类别:save")
    @PreAuthorize("hasAuthority('archive:class:save')")
    public Result save(@RequestBody ArchiveBaseCategory archiveBaseClass){
        //效验数据
        ValidatorUtils.validateEntity(archiveBaseClass, AddGroup.class, DefaultGroup.class);

        archiveBaseCategoryService.insert(archiveBaseClass);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("全宗类别:update")
    @PreAuthorize("hasAuthority('archive:class:update')")
    public Result update(@RequestBody ArchiveBaseCategory archiveBaseClass){
        //效验数据
        ValidatorUtils.validateEntity(archiveBaseClass, UpdateGroup.class, DefaultGroup.class);

        archiveBaseCategoryService.updateById(archiveBaseClass);

        return new Result();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    @LogOperation("全宗类别:delete")
    @PreAuthorize("hasAuthority('archive:class:delete')")
    public Result delete(@PathVariable String id){
        ArchiveBaseCategory olsArchiveBaseCategory = archiveBaseClassMapper.selectById(id);
        if ( CollectionUtils.isNotEmpty(archiveBaseClassMapper.selectList(new QueryWrapper<ArchiveBaseCategory>().eq("pid", olsArchiveBaseCategory.getId())))
        ) {
            return new Result().error("该节点下面存在子节点不能删除");
        }
        archiveBaseCategoryService.deleteById(id);
        return new Result();
    }

}
